/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年3月7日         luwei
 */

package com.coocaa.app.gamecenter.pages.home;

import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.coocaa.app.gamecenter.pages.detail.DetailActivity;
import com.coocaa.launcher.framework.sidebar.item.Item;
import com.coocaa.launcher.framework.sidebar.item.ItemView;
import com.coocaa.x.app.framework.app.LiteAppApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.framework.utils.NativeImageCache;
import com.coocaa.x.framework.utils.NativeImageCache.SmartBitmap;
import com.coocaa.x.service.litecontent.LiteContentServiceConnecter;
import com.skyworth.framework.skysdk.ipc.SkyStartActivity;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.widget.TipToast;
import com.tianci.appstore.utils.SkyConstants;

import java.util.HashMap;
import java.util.Map;

public class RecommendItem extends Item {
    private SkyListAppData mAppData = null;

    public RecommendItem(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    public ItemView createItemView() {
        // TODO Auto-generated method stub
        return new RecommendItemView(mContext);
    }

    @Override
    public void onItemClick(View v) {
        if (LiteAppApplication.checkNetworkConnection(TipToast.showNetIsNotConnectedToastRunnable)) {
            LogUtils.i("luwei", "the click id is " + mAppData.getId());
            LogUtils.i("luwei", "the click pkgname is " + mAppData.getPkgName());
            if (mAppData.getUri() != null && !mAppData.getUri().equals("")
                    && !mAppData.getUri().equals("null")) {
                LogUtils.i("luwei", "the category uri is " + mAppData.getUri());
                try {
                    LiteContentServiceConnecter.getService().call(mAppData.getUri());
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }else{
                Intent intent = new Intent("com.coocaa.gamecenter.detail");
                int appid = -1;
                try {
                    appid = Integer.valueOf(mAppData.getId());
                } catch (NumberFormatException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                intent.putExtra(DetailActivity.INTENT_APPID_KEY, appid);
                intent.putExtra(DetailActivity.INTENT_PACKAGENAME_KEY, mAppData.getPkgName());
//            mContext.startActivity(intent);
                HashMap<String,String> map = new HashMap<String,String>();
                SkyStartActivity.startActivity(mContext, mContext.getClass().getName(), intent, map, true);
            }
            Map<String,String> params = new HashMap<String, String>();
            params.put("id",getLogPosition());
            params.put("category","推荐页");
            params.put("block_content_name", mAppData.getTitle());
            params.put("block_content_id", mAppData.getPackageName());
            LogUtils.submit(LogUtils.PRODUCTID_APPSTORE, "gc2_main_block_onclick", params);
        }
        super.onItemClick(v);
    }

    private String getLogPosition(){
        switch (mIndex){
            case 0:
                return "11";
            case 1:
                return "12";
            case 2:
                return "21";
            case 3:
                return "22";
            case 4:
                return "23";
        }
        return "";
    }

    @Override
    public void setData(Object object) {
        mAppData = (SkyListAppData) object;
        LogUtils.d("pic", "setData Title:" + mAppData.getTitle() + " url:" + mAppData.getIconUrl());
        if (mAppData.getIconUrl() != null && !mAppData.getIconUrl().equals("")) {
            if (mIndex >= 2) {
                ((RecommendItemView) (mItemView)).updateIcon(mAppData.getIconUrl(), Div(455), Div(255));
            } else {
                ((RecommendItemView) (mItemView)).updateIcon(mAppData.getIconUrl(), Div(730), Div(415));
            }
        }
        ((RecommendItemView) (mItemView)).updateName(mAppData.getTitle());
        if (mAppData.getControll() != null && mAppData.getControll().size() > 0
                && mAppData.getControll().get(0) != null && !mAppData.getControll().get(0).equals("")) {
            JSONObject jb = JSONObject.parseObject(mAppData.getControll().get(0));
            String device = jb.getString("device");
            LogUtils.i("luwei", "the device is " + device);
            String[] devices = device.split(",");
            int len = devices.length;
            if (len > 0) {
                SmartBitmap[] mSmartBitmaps = new SmartBitmap[len];
                SmartBitmap bitmap = null;
                for (int j = 0; j < len; j++) {
                    int id = SkyConstants.getIconId(devices[j]);
                    if (id != R.drawable.pic_nopic) {
                        bitmap = NativeImageCache.getInstance().getBitmapById(mContext.getResources(), id, Div(50), Div(50));
                        mSmartBitmaps[j] = bitmap;
                    }
                }
                ((RecommendItemView) (mItemView)).updateControlIcon(mSmartBitmaps);
            }
        }
        super.setData(object);
    }

    @Override
    public void destory() {
        mItemView.destory();
        super.destory();
    }

}
